package com.sun.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.reggie.Exception.CustomException;
import com.sun.reggie.entity.Category;
import com.sun.reggie.entity.Dish;
import com.sun.reggie.entity.Setmeal;
import com.sun.reggie.mapper.CategoryMapper;
import com.sun.reggie.service.CategoryService;
import com.sun.reggie.service.DishService;
import com.sun.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService{
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;
    @Override
    public void remove(Long id) {

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);

        int dishCount = dishService.count(dishLambdaQueryWrapper);
        if(dishCount > 0){
            throw new CustomException("此分类下有多个菜品，无法删除");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);

        int setmealCount = dishService.count(dishLambdaQueryWrapper);
        if(setmealCount > 0){
            throw new CustomException("此分类下有多个套餐，无法删除");
        }

        super.removeById(id);
    }
}